package com.hoshiicloud.stock.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hoshiicloud.common.controller.BaseController;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.stock.entity.Warehouse;
import com.hoshiicloud.stock.param.WarehousePageParam;
import com.hoshiicloud.stock.param.WarehouseUpdateParam;
import com.hoshiicloud.stock.service.IWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 仓库controller类
 *
 * @author : yangcheng
 * @date : 2019/5/20 14:23
 * @mail : maiboyc@163.com
 **/
@Slf4j
@RestController
@RequestMapping("/warehouse")
@Api(value = "仓库管理", tags = {"仓库操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class WarehouseController extends BaseController {

    // 仓库Service
    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 根据ID查询仓库信息
     * @param id 仓库ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询仓库信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "仓库主键ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<Warehouse> getById(@PathVariable("id") Long id) {
        Response<Warehouse> rs;
        try {
            rs = Response.data(warehouseService.getById(id));
        } catch (Exception e) {
            log.error("根据ID查询仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("根据ID查询仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 根据ID组查询仓库信息
     * @param ids ID组
     * @return Response
     */
    @GetMapping("/getByIds")
    @ApiOperation(value = "根据ID组查询", notes = "根据ID组查询仓库信息", consumes = MediaType.ALL_VALUE)
    public Response<List<Warehouse>> getByIds(@RequestParam("ids") List<Long> ids) {
        Response<List<Warehouse>> rs;
        try {
            rs = Response.data(warehouseService.getByIds(ids));
        } catch (Exception e) {
            log.error("根据ID组查询仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("根据ID组查询仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 根据仓库代码查询仓库信息
     * @param code  仓库代码
     * @return Response
     */
    @GetMapping("/find/{code}")
    @ApiOperation(value = "根据仓库代码查询", notes = "根据仓库代码查询仓库信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "code", value = "仓库代码", required = true, dataType = "String", example = "WH0001", paramType = "query")
    public Response<IPage<Warehouse>> findByCode(@PathVariable("code") String code) {
        Response<IPage<Warehouse>> rs;
        try {
            Warehouse entity = new Warehouse();
            entity.setWhcode(code);
            rs = Response.data(warehouseService.List(entity));
        } catch (Exception e) {
            log.error("根据仓库代码查询仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("根据仓库代码查询仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 分页查询仓库信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询仓库信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<IPage<Warehouse>> list(@RequestBody WarehousePageParam param) {
        Response<IPage<Warehouse>> rs;
        try {
            Warehouse entity = new Warehouse();
            BeanUtils.copyProperties(param, entity);
            rs = Response.data(warehouseService.List(entity));
        } catch (Exception e) {
            log.error("分页查询仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("分页查询仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 新增仓库
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增仓库", notes = "新增仓库信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> save(@RequestBody @Valid Warehouse entity, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(warehouseService.insert(entity));
            }
        } catch (ServiceException e) {
            log.error("新增仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        }catch (Exception e) {
            log.error("新增仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("新增仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 修改仓库
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改仓库", notes = "修改仓库信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> update(@RequestBody @Valid WarehouseUpdateParam param, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(JSON.toJSONString(handleValidMsg(result)));
            } else {// 验证通过
                Warehouse entity = new Warehouse();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(warehouseService.update(entity));
            }
        } catch (ServiceException e) {
            log.error("更新仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            log.error("更新仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("更新仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

    /**
     * 根据ID删除仓库信息
     * @param id 仓库ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除仓库信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "仓库主键ID", required = true, dataType = "Long", example = "3187243209717710848", paramType = "query")
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        Response<Boolean> rs;
        try {
            rs = Response.data(warehouseService.delete(id));
        } catch (ServiceException e) {
            log.error("删除仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            log.error("删除仓库信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed("删除仓库信息异常"+"["+e.getMessage()+"]");
        }
        return rs;
    }

}
